package com.efast.dmom.aps.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.aps.bean.Equipment;
import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.bean.WorkCenter;
import com.efast.dmom.aps.bean.Workshop;
import com.efast.dmom.aps.datahandlers.EquipmentDataHandler;
import com.efast.dmom.aps.service.EquipmentService;
import com.efast.dmom.aps.util.StringVerifyUtil;

/**
* @author Aaron Liu
* @version Creation Time：2020年2月4日 下午7:04:07
*/
@Service
public class EquipmentServiceImpl implements EquipmentService {

	@Autowired
	EquipmentDataHandler equipmentDataHandler = null;

	public EquipmentServiceImpl() {
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.efast.dmom.aps.service.EquipmentService#saveEquipmentList(java.util.
	 * List)
	 */
	@Override
	public void saveEquipmentList(List<Equipment> equipList) {

		equipmentDataHandler.saveEquipmentList(equipList);

	}

	@Override
	public ResultBean save(Equipment equipment) {
		ResultBean resultBean = new ResultBean(true,"新增成功！");
		if (!StringUtil.isNotBlank(equipment.getInnerData().getName())
				&& !StringUtil.isNotBlank(equipment.getInnerData().getEquipmentCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查设备名称、设备编码是否填写!");
			return resultBean;
			
		}	
		
		if (!StringVerifyUtil.isstring(equipment.getInnerData().getEquipmentCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("设备编码不是由英文、字母、下划线组成!!");
		    return resultBean;
		   }
		if (equipment.getInnerData().getWorkCenterId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工厂id不能为空!");
			return resultBean;
		}
		Equipment equipmentByName = equipmentDataHandler.getByName(equipment.getInnerData().getName());
		Equipment equipmentByCode = equipmentDataHandler.getByCode(equipment.getInnerData().getEquipmentCode());
		if (equipmentByName.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("设备称不唯一!");
			return resultBean;
		}
		if (equipmentByCode.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("设备编码不唯一!");
			return resultBean;
		}
		equipmentDataHandler.save(equipment);
		return resultBean;
	}

	@Override
	public ResultBean update(Equipment equipment) {
		ResultBean resultBean = new ResultBean(true,"编辑成功！");
		Equipment equipmentById = equipmentDataHandler.getById(equipment.getInnerData().getId());
//		if (equipmentById.getInnerData() == null) {
//			resultBean.setSuccess(false);
//			resultBean.setMessage("修改的设备不存在!");
//			return resultBean;
//		}
		if (!StringVerifyUtil.isstring(equipment.getInnerData().getEquipmentCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("设备编码不是由英文、字母、下划线组成!!");
		    return resultBean;
		   }
		if (!equipment.getInnerData().getEquipmentCode().equals(equipmentById.getInnerData().getEquipmentCode())
				&& !equipment.getInnerData().getName().equals(equipmentById.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("设备编码、设备名称不可修改!");
			return resultBean;
		}
		equipmentDataHandler.update(equipment);
		return resultBean;
	}

	@Override
	public ResultBean delete(Equipment equipment) {
		ResultBean resultBean = new ResultBean(true,"删除成功！");
		equipmentDataHandler.delete(equipment);
		return resultBean;
	}

	@Override
	public Equipment getById(long id) {
		Equipment byId = equipmentDataHandler.getById(id);
		return byId;
	}

	@Override
	public Equipment getByName(String name) {
		Equipment byName = equipmentDataHandler.getByName(name);
		return byName;
	}

	@Override
	public Equipment getByCode(String workshopCode) {
		Equipment byCode = equipmentDataHandler.getByCode(workshopCode);
		return byCode;
	}

}
